package com.csw;

import com.csw.pojo.Brand;
import com.csw.pojo.Cell;
import com.csw.pojo.Layer;
import com.csw.pojo.Map;
import com.csw.util.LayerUtil;
import com.csw.util.MapUtil;
import com.csw.util.Music;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2022/11/28 23:05
 */
public class GameFrame extends JFrame {

    public static Map map=MapUtil.built(3);
    public GameFrame() throws Exception {

        this.setTitle("羊了个羊");
        this.setSize(400, 800);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        //this.renderLayer();
        this.renderMap();
        this.setVisible(true);
        map.compareAll();
        autoRefresh();
        new Music().music();
    }

    //自动刷新线程
    private void autoRefresh(){
        final GameFrame gameFrame=this;
        new Thread(new Runnable() {
            public void run() {
                while (true){
                    gameFrame.repaint();
                    try {
                        Thread.sleep(40);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    //添加brand 绘制brand
    private void renderLayer(Layer layer){
        //添加组件的方法
        //this.getContentPane().add(brand);
        Cell[][] cells = layer.getCells();
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                Brand brand = cells[i][j].getBrand();
                int x=i*50+layer.getOffsetX();
                int y=j*50+layer.getOffsetY();
                //继承父类的方法
                brand.setBounds(x, y, 50, 50);
                this.getContentPane().add(brand);
            }
        }

    }
    //绘制map
    private void renderMap(){
        List<Layer> layerList = map.getLayerList();
        for (int f = 0; f < layerList.size(); f++) {
            Layer layer = layerList.get(f);

            layer.show();
            System.out.println("==========");
            renderLayer(layer);
        }
    }
    public static void main(String[] args) throws Exception {
        new GameFrame();
    }
}
